package com.smartview.jboss.spring.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.smartview.jboss.spring.dto.UserDTO;
import com.smartview.jboss.spring.dto.WeatherDTO;

@Repository
@Transactional
public class UserDao {
	// We need to use the entity manager factory bean provided by spring framework
	@PersistenceContext
	private EntityManager em;
	
	@Transactional(readOnly = true)
	public UserDTO getAccount(String id) {
		// Here, I am going to the DB to grab the weather info
		// make it as an instance of weather,and then return it to you.
		return em.find(UserDTO.class, id);
	}
	
/*	public void saveWeather(WeatherDTO weather) {
		// You give me the weather information, and I will save it to the DB
		em.persist(weather);
	}
*/
	@Transactional(readOnly = true)
	public UserDTO getUserData() {
		@SuppressWarnings("unchecked")
		UserDTO dto = (UserDTO)em.createQuery("select data from UserDTO data").getResultList().get(0);
		return dto;
	}
	
	@Transactional(readOnly = true)
	public UserDTO getCheckUser(String id){
		@SuppressWarnings("unchecked")
		UserDTO dto = (UserDTO)em.createQuery("select data from UserDTO data where id= :id").setParameter("id", id).getResultList().get(0);
		return dto;
	}
	
	public void saveAccount(UserDTO dto) {
		// You give me the weather information, and I will save it to the DB
		em.persist(dto);
	}
}
